package com.markedquilt.solution;
/*
 * PackageName: com.markedquilt.solution
 * ClassName:   num141HasCycle
 * Author:   zhang
 * Date:     Created in 2020/10/22
 * Description:
 * History:
 * <author>        <time>             <version>          <desc>
 * 作者姓名         修改时间            版本号              描述
 * zhang           2020/10/22      1.0
 */

import com.markedquilt.solution.entity.ListNode;

/**
 * Title:
 * Project: LeetcodeProblem
 *
 * @author ZhangKe
 * Created on 2020/10/22
 */
public class num141HasCycle {
    public static boolean hasCycle(ListNode head) {
        ListNode slow = head;
        ListNode fast = head;
        if (head.next==null)
            return false;
        else if (head.next.next==null)
            return false;
        else{
            while(true){
                if (fast.next==null || fast.next.next==null)
                    return false;
                else if (fast.next==slow || fast.next.next==slow)
                    return true;
                else{
                    slow = slow.next;
                    fast = fast.next.next;
                }
            }
        }
    }

    public static void main(String[] args) {
        ListNode listNode1 = new ListNode(3);
        ListNode listNode2 = new ListNode(2);
        listNode1.next = listNode2;
        ListNode listNode3 = new ListNode(1);
        listNode2.next = listNode3;
        listNode3.next = listNode1;
        ListNode head = new ListNode(0);
        head.next = listNode1;
        System.out.println(hasCycle(head));
    }
}
